﻿/*********************************************************************************
 *Author:         OnClick
 *Version:        1.0
 *UnityVersion:   2018.3.11f1
 *Date:           2019-04-16
 *Description:    IFramework
 *History:        2018.11--
*********************************************************************************/
using UnityEditor;
using System.IO;
using System.Collections.Generic;

namespace IFramework
{
    public class ABCollectInfoIO
    {
        private static string StoPath = FrameWorkInfo.GetFrameWorkPath().CombinePath("Tools/Unity/AssetBundle/Editor/Window/Sto/ABCollectInfoSto.asset");
        public static void Save(List<ABCollectViewItem> infos)
        {
            if (!File.Exists(StoPath))
                ScriptableObj.Create<ABCollectInfoSto>(StoPath);
            ABCollectInfoSto sto = ScriptableObj.Load<ABCollectInfoSto>(StoPath);
            sto.Data.infos.Clear();
            sto.Data.infos.AddRange(infos);
            ScriptableObj.Update<ABCollectInfoSto>(sto);
        }
        public static List<ABCollectViewItem> Load()
        {
            if (!File.Exists(StoPath))
                ScriptableObj.Create<ABCollectInfoSto>(StoPath);
            return ScriptableObj.Load<ABCollectInfoSto>(StoPath).Data.infos;
        }
        public static List<ABBuildCollecter> LoadCollecter()
        {
            if (!File.Exists(StoPath))
            {
                ScriptableObj.Create<ABCollectInfoSto>(StoPath);
                EditorUtility.DisplayDialog("Warning", "Please Set CollectInfos Before Build", "ok");
                ABWindow.IsShowCollect = true;
            }
            List<ABCollectViewItem> infos= ScriptableObj.Load<ABCollectInfoSto>(StoPath).Data.infos;
            List<ABBuildCollecter> collecters = new List<ABBuildCollecter>();
            for (int i = 0; i < infos.Count; i++)
            {
                if (string.IsNullOrEmpty(infos[i].searchPath)) continue;
                switch (infos[i].collectType)
                {
                    case ABCollectType.ABName:
                        if (!string.IsNullOrEmpty(infos[i].bundleName) )
                        {
                            collecters.Add(new CollectByABName(infos[i].searchPath,
                                                       infos[i].searchPattern, infos[i].searchOption, infos[i].bundleName));
                        }
                        break;
                    case ABCollectType.DirName:
                        collecters.Add(new CollectByDirName(infos[i].searchPath,
                            infos[i].searchPattern, infos[i].searchOption));
                        break;
                    case ABCollectType.FileName:
                        collecters.Add(new CollectByFileName(infos[i].searchPath,
                            infos[i].searchPattern, infos[i].searchOption));
                        break;
                    case ABCollectType.Scene:
                        collecters.Add(new CollectByScenes(infos[i].searchPath,
                            infos[i].searchPattern, infos[i].searchOption));
                        break;
                    default:
                        break;
                }
            }
            if (collecters.Count == 0)
            {
                EditorUtility.DisplayDialog("Warning", "Please Set CollectInfos Before Build", "ok");
                ABWindow.IsShowCollect = true;
            }
            else
            {
                ABWindow.IsShowCollect = false;
            }
            return collecters;
        }
    }
}
